/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.materialMedia.material.LinearElasticIsotropic;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A class representing creation dialog on screen.
 * For example:
 * <pre>
 *    ElmTempChangesDialog dgl = new ElmTempChangesDialog(parent);
 *    dgl.pack();
 * </pre>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public final class ElmTempChangesDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	
	private DecimalField TsField;
	private DecimalField TnField;
	private DecimalField TiField;
	public double Ts;
	public double Tn;
	public double Ti;
	
//*****************************************************************************
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.InsaneInternalInterface
	 */
	public ElmTempChangesDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		
		setUpFormat();
		setUpFields();
		
//		Sets up the temperature panel
		JPanel temperaturePane = new JPanel();
		temperaturePane.setPreferredSize(new Dimension(360,240));
		temperaturePane.setLayout(new BoxLayout(temperaturePane, BoxLayout.Y_AXIS));
		
		JPanel tempPicPane = new JPanel();
		tempPicPane.setMaximumSize(new Dimension(500,100));
		String imgName = "br/ufmg/dees/insane/images/" + parent.getResourceBundle().getString("imgThermal100");
		tempPicPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource(imgName)) ));
		temperaturePane.add(tempPicPane);
		
		JPanel tempPane = new JPanel();
		tempPane.setLayout(new GridLayout(3,1,2,2));
        tempPane.setMaximumSize(new Dimension(500,120));
		tempPane.setBorder(BorderFactory.createCompoundBorder(
        	BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dETempChanDialog01")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		
		JPanel tempTSPane = new JPanel();
		tempTSPane.add(new JLabel("Ts:"));
		tempTSPane.add(TsField);
		tempPane.add(tempTSPane);
		
		JPanel tempTNPane = new JPanel();
		tempTNPane.add(new JLabel("Tn:"));
		tempTNPane.add(TnField);
		tempPane.add(tempTNPane);
		
		JPanel tempTIPane = new JPanel();
		tempTIPane.add(new JLabel("Ti:"));
		tempTIPane.add(TiField);
		tempPane.add(tempTIPane);
		
		temperaturePane.add(tempPane);
		
		setPane(temperaturePane);
		
    }
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(parent, parent.getResourceBundle().getString("dETempChanDialog02"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		
		try {
			Ts = TsField.getValue();
			Tn = TnField.getValue();
			Ti = TiField.getValue();
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		
		if (this.parent.getController().getSelection().size() == 1 ) {
			
			FrameElement elm = (FrameElement)this.parent.getController().getSelection().get(0);
			
			//Setting temperature fields
			TsField = new DecimalField(elm.getDeltaTemp(0), 7, numberFormat);
			TnField = new DecimalField(elm.getDeltaTemp(1), 7, numberFormat);
			TiField = new DecimalField(elm.getDeltaTemp(2), 7, numberFormat);
			
			if (elm.getCrossSection()!=null && elm.getCrossSection().getHeight()!=0 && elm.getMaterial()!=null && ((LinearElasticIsotropic)elm.getMaterial()).getMaterialValues(((LinearElasticIsotropic)elm.getMaterial()).THERMAL_COEFF).doubleValue()!=0) {
				if (elm.getAnalysisModel().getType().equals("Beam")) {
					TsField.setEnabled(true);
					TnField.setEnabled(false);
					TiField.setEnabled(true);
				}else if (elm.getAnalysisModel().getType().equals("PlaneFrame")) {
					TsField.setEnabled(true);
					TnField.setEnabled(true);
					TiField.setEnabled(true);
				} else if (elm.getAnalysisModel().getType().equals("PlaneTruss")) {
					TsField.setEnabled(false);
					TnField.setEnabled(true);
					TiField.setEnabled(false);
				} else if (elm.getAnalysisModel().getType().equals("Grid")) {
					TsField.setEnabled(true);
					TnField.setEnabled(false);
					TiField.setEnabled(true);
				}
			} else {
				TsField.setEnabled(false);
				TnField.setEnabled(false);
				TiField.setEnabled(false);
			}
			
		} else {
			
			//Setting temperature fields
			TsField = new DecimalField(0, 7, numberFormat);
			TnField = new DecimalField(0, 7, numberFormat);
			TiField = new DecimalField(0, 7, numberFormat);
			TsField.setEnabled(false);
			TnField.setEnabled(false);
			TiField.setEnabled(false);
			
		}
		
	}
	
//*****************************************************************************
}

